package com.zzyk.microCloud.api.controller;

import com.github.pagehelper.PageHelper;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.PageView;
import com.zzyk.main.model.vo.WxUser;
import com.zzyk.main.service.ISysUserService;
import com.zzyk.main.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public abstract class ApiBaseController {
    private static final String OPEN_ID = "openId";

    @Autowired
    protected ISysUserService userService;

    public WxUser getUser() throws UserException {
        String openId = SpringUtils.getParameter(OPEN_ID);
        if (!StringUtils.isEmpty(openId)) {
            WxUser user = userService.getWxUserByOpenId(openId);
            if (user == null) {
                throw new UserException("用户不存在");
            } else {
                user.setSysUser(userService.getUserDetails(user.getUserInfo().getUsername()));
                return user;
            }
        } else {
            throw new UserException("用户不存在");
        }
    }

    protected void setPage() {
        PageView pageView = PageView.getPageView();
        Integer pageIndex = pageView.getPageIndex();
        Integer pageSize = pageView.getPageSize();
        if (pageIndex != null && pageSize != null) {
            String orderBy = pageView.getOrderBy();
            PageHelper.startPage(pageIndex, pageSize, orderBy);
        }
    }
}
